<template>
  <div class="messageConfig h100">
    <div class="table-list">
      <div class="table-container">
        <list-table-new ref="klTable" v-bind="tableSetting"></list-table-new>
      </div>
    </div>
  </div>
</template>

<script>
import {
  queryHistoryList,
  queryMessageList,
} from "@/api/common.js"
import {
  applicationQueryList
} from '@/api/systemManagement/applicationManagement.js'
export default {
  data(){
    return {
      deptList: [],
      contentList: [],
      choiceDate0: '',
      tableSetting: {
        radioShow: false,
        showCustomBtn: true, //显示自定义列按钮
        showFilterBtn: true, //显示过滤器按钮
        pageConfig: {
          pageCode: "noticeRecord",
          userId: this.$store.getters.userInfo.id,
        },
        searchObj: {
          searchMethod: (params, sortStr) => {
            this.getTableData(params,sortStr);
          },
          queryData: {
            page: 1,
            pageSize: 15,
            appMark: this.$store.getters.userInfo.currDs
          },
          searchArr: [
            {
              type: "select",
              label: "所属租户",
              model: "appMark",
              option: []
            },
            {
              type: "select",
              label: "消息主题",
              model: "topicCode",
              option: []
            },
            {
              type: "select",
              label: "通知方式",
              model: "notifyType",
              option: this.$store.getters.dirtData("message_notify_type")
            },
            {
              type: "input",
              label: "消息内容",
              model: "content"
            },
            {
              type: "datetimerange",
              label: "发送时间",
              model: "sendTime",
              pickerOptions: {
                onPick: ({ maxDate, minDate }) => {
                  this.choiceDate0 = minDate.getTime();
                  if (maxDate) {
                    this.choiceDate0 = "";
                  }
                },
                disabledDate: (time) => {
                  let choiceDateTime = new Date(this.choiceDate0).getTime();
                  const minTime = new Date(choiceDateTime).setMonth(
                    new Date(choiceDateTime).getMonth() - 1
                  );
                  const maxTime = new Date(choiceDateTime).setMonth(
                    new Date(choiceDateTime).getMonth() + 1
                  );
                  const min = minTime;
                  const newDate =
                    new Date(new Date().toLocaleDateString()).getTime() +
                    24 * 60 * 60 * 1000 -
                    1;
                  const max = newDate < maxTime ? newDate : maxTime;
                  if (this.choiceDate0) {
                    return time.getTime() < min || time.getTime() > max;
                  }
                  return time.getTime() > newDate;
                },
              },
            },
            {
              type: "select",
              label: "发送状态",
              model: "status",
              option: [
                {
                  label: "成功",
                  value: true
                },
                {
                  label: "失败",
                  value: false
                }
              ]
            },
            {
              type: "userOnly",
              label: "接收人",
              model: "handlerName",
              idsStr: "handler",
              multiple: true
            },
          ],
        },
        actionList: [],
        column: [
          {
            label: "所属租户",
            prop: "appMark",
            sortable: "custom",
            formatter: row => {
              let value = row.appMark;
              let str = "";
              this.deptList.map(i=>{
                if(value==i.value){
                  str = i.label;
                }
              })
              return str
            },
          },
          {
            label: "消息主题",
            prop: "topicName",
            sortable: "custom"
          },
          {
            label: "通知方式",
            prop: "notifyType",
            sortable: "custom",
            formatter: row => {
              let value = row.notifyType;
              return (
                this.$store.getters["fineDirtDataName"](
                  "message_notify_type",
                  value
                ) || value
              );
            },
          },
          {
            label: "消息内容",
            prop: "content",
            sortable: "custom",
            scopeType: "formate",
            method: (row)=>{
              return row.content;
            }
          },
          {
            label: "发送时间",
            prop: "createTime",
            sortable: "custom"
          },
          {
            label: "发送状态",
            prop: "status",
            sortable: "custom",
            formatter: row=>{
              return row.status?"成功":"失败";
            }
          },
          {
            label: "接收人",
            prop: "handlerName",
            sortable: "custom"
          },
        ],
        // 表格数据
        tableData: [],
        //表格方法
        tableMethods: {
          //点击行的方法
          rowClick: row => {
          }
        }
      }
    }
  },
  created(){
    /* 添加监听事件 */
    window.addEventListener("storage", this.updateData);
    applicationQueryList().then(res=>{
      res.data.map(i=>{
        this.deptList.push({label: i.name,value: i.mark})
      })
      this.tableSetting.searchObj.searchArr[0].option = this.deptList;
    })
    queryMessageList().then(res=>{
      let option = res.data.list.map(i=>{
        let obj = {
          label: i.topicName,
          value: i.topicCode
        }
        return obj;
      })
      this.tableSetting.searchObj.searchArr[1].option = option;
    })
  },
  beforeDestroy() {
    /* 删除监听事件 */
    window.removeEventListener("storage", this.updateData);
  },
  mounted(){
    this.init();
  },
  methods: {
    init(){
      this.$refs.klTable.getTableConfig();
    },
    updateData() {
      this.$refs.klTable.searchData();
    },
     getTableData(params,sortStr){
      let form = { ...params };
      if (sortStr) {
        form.orders = sortStr;
      }
      // 处理发送时间
      if(form.sendTime&&form.sendTime.length){
        form.startSendTime = form.sendTime[0];
        form.endSendTime = form.sendTime[1];
      }
      delete form.sendTime;
      delete form.handler;
      // 调用接口请求数据
      queryHistoryList(form)
        .then(res => {
          this.$set(this.tableSetting, "tableData", res.data.list);
        this.$set(this.tableSetting, "totalNumber", res.data.total);
        })
        .catch(e => {});
    },
  }
}
</script>

<style lang="less" scoped>
  .messageConfig{
    width: 100%;
    height: 100%;
    background-color: white;
    .table-list {
      height: calc(100% - 10px);
      width: 100%;
      padding: 20px;
      .table-container {
        width: 100%;
        height: calc(100% - 10px); 
      }
    }
  }
</style>